package com.yanqu.road.server.logic.activity.condition.normal;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.activity.args.RechargeVipArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.utils.TimeHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 *  充值登陸活动(活動開始)
 */
public class RechargeVipDayCondition extends BaseActivityCondition {

	public RechargeVipDayCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.RechargeVipExpDay.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.RechargeVipExpDay.getValue(), handle);
	}

	public boolean isCompleted(GamePlayer player) {
		UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(getConditionId());
		return isCompleted(userActivityConditionData, activityConditionInfo);
	}

	public static boolean isCompleted(UserActivityConditionData data, ActivityConditionInfo activityConditionInfo) {
		List<Integer> paramList = StringUtils.stringToIntegerList(data.getParam(),"\\|");
		//充值额度和天数同时满足条件
		int param1 = 0;
		int param2 = 0;
		if(paramList.size() > 1){
			param1 = paramList.get(0);
			param2 = paramList.get(1);
		}
		return BigInteger.valueOf(param1).compareTo(activityConditionInfo.getParamList().get(2)) >= 0
				&& BigInteger.valueOf(param2).compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		RechargeVipArgs args = (RechargeVipArgs)object;
		List<Integer> paramList = StringUtils.stringToIntegerList(getParam(),"\\|");
		int param1 = 0;
		if(paramList.size() > 0){
			param1 = paramList.get(0);
		}
		long vipExp = param1 + args.getVipExp();
		long day = TimeHelper.dateCompare(new Date(activityInfo.getBeginTime()*1000),new Date()) + 1;
		setParam(vipExp + "|" + day);
		setValue(BigInteger.ZERO);
	}

	@Override
	public void initRechargeConditionData() {
		player.getModule(EarnSpeedModule.class).resetDay();
		initParam();
	}

	private void initParam(){
		if ("".equals(getParam())) {
			long recharge = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.DayTotalRecharge);
			changeValue(new RechargeVipArgs(recharge, 1));
		}
	}
}
